﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.system.order
{
    public partial class tblist : System.Web.UI.Page
    {
        Maticsoft.BLL.Orders bll = new Maticsoft.BLL.Orders();
        BLL.DealersManage dmbll = new BLL.DealersManage();
        Maticsoft.BLL.UA_Account uabll = new Maticsoft.BLL.UA_Account();
        Maticsoft.BLL.comment cmtbll = new Maticsoft.BLL.comment();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidwhere.Value = "orderstatus<>0";
                int totalOrders = bll.GetRecordCount(hidwhere.Value);
                AspNetPager1.RecordCount = totalOrders;
                bindData(hidwhere.Value);
                //this.source.DataSource = uabll.GetList("");
                //this.source.DataTextField = "cAcc_Name";
                //this.source.DataValueField = "cAcc_Id";
                //this.source.DataBind();
                //this.source.Items.Insert(0, new ListItem("--请选择数据源--", ""));
            }
        }
        protected string getcmt(string ordercode, string orderstatus)
        {
            List<Maticsoft.Model.comment> cmts = cmtbll.GetModelList("ordercode='" + ordercode + "'");
            if (orderstatus == "5" && cmts != null && cmts.Count > 0)
            {
                return "<a title=\"查看评价\" href=\"javascript:;\" onclick=\"pingjiat('查看评价','comment.aspx','" + Eval("code") + "&action=get',550,400)\" style=\"text-decoration: none\">查看评价</a>";
            }
            else if (orderstatus == "5")
            {
                return "<a title=\"评价\" href=\"javascript:;\" onclick=\"pingjiat('评价','comment.aspx','" + ordercode + "',550,400)\" style=\"text-decoration: none\">评价</a>";
            }
            return "";
        }
        void bindData(string where)
        {
            int totalOrders = bll.GetRecordCount(where);
            AspNetPager1.RecordCount = totalOrders;
            Repeater1.DataSource = bll.GetListByPage(where, "", AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
            Repeater1.DataBind();
        }
        //查询门店
        protected string getmd(string DealersManageID)
        {
            return dmbll.GetModel(Convert.ToInt32(DealersManageID)).username;
        }
        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            bindData(hidwhere.Value);
        }

        protected void btnserach_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["UserLogin"];
            string where = "orderstatus<>0";
            if (!string.IsNullOrEmpty(startTime.Value) && !string.IsNullOrEmpty(endTime.Value))
            {
                where += " and createtime between '" + startTime.Value + "' and '" + endTime.Value + "'";
            }
            if (!string.IsNullOrEmpty(mobile.Value))
            {
                where += " and membermobile like '%" + mobile.Value + "%'";
            }
            if (!string.IsNullOrEmpty(code.Value))
            {
                where += " and code like '%" + code.Value + "%'";
            }
            hidwhere.Value = where;
            bindData(hidwhere.Value);
        }
        //private string getwhere()
        //{
        //    HttpCookie cookie = Request.Cookies["UserLogin"];
        //    return "orderstatus<>0 and DealersManageID='" + cookie.Values["dmid"] + "' ";
        //}
        //今天
        protected void btnday_Click(object sender, EventArgs e)
        {
            bindData(string.Format("orderstatus<>0 and createtime between '{0}' and '{1}'", DateTime.Now.ToString("yyyy-MM-dd 00:00:00"), DateTime.Now.ToString("yyyy-MM-dd 23:59:59")));
        }
        //本周
        protected void btnweeks_Click(object sender, EventArgs e)
        {
            int weeknow = Convert.ToInt32(System.DateTime.Now.DayOfWeek);
            //星期日 获取weeknow为0  
            weeknow = weeknow == 0 ? 7 : weeknow;
            int daydiff = (-1) * weeknow + 1;
            int dayadd = 7 - weeknow;
            bindData(string.Format("orderstatus<>0 and createtime between '{0}' and '{1}'", System.DateTime.Now.AddDays(daydiff).ToString("yyyy-MM-dd 00:00:00"), System.DateTime.Now.AddDays(dayadd).ToString("yyyy-MM-dd 23:59:59")));
            AspNetPager1.CurrentPageIndex = 1;
        }
        //本月
        protected void btnmonth_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            //本月第一天时间   
            DateTime dt_First = dt.AddDays(-(dt.Day) + 1);
            //将本月月数+1   
            DateTime dt2 = dt.AddMonths(1);
            DateTime dt_Last = dt2.AddDays(-(dt.Day));
            bindData(string.Format("orderstatus<>0 and createtime between '{0}' and '{1}'", dt_First.ToString("yyyy-MM-dd 00:00:00"), dt_Last.ToString("yyyy-MM-dd 23:59:59")));
            AspNetPager1.CurrentPageIndex = 1;
        }
        //最近三月
        protected void btntree_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            //本月第一天时间   
            DateTime dt_First = dt.AddMonths(-3).AddDays(-(dt.Day) + 1);
            //将本月月数+3
            DateTime dt2 = dt.AddMonths(1);
            DateTime dt_Last = dt2.AddDays(-(dt.Day));
            bindData(string.Format("orderstatus<>0 and createtime between '{0}' and '{1}'", dt_First.ToString("yyyy-MM-dd 00:00:00"), dt_Last.ToString("yyyy-MM-dd 23:59:59")));
            AspNetPager1.CurrentPageIndex = 1;
        }
        //已付款
        //protected void btnpay_Click(object sender, EventArgs e)
        //{
        //    bindData(string.Format("orderpaystatus={0}", 1));
        //}
        private string getorderpaytype()
        {
            if (orderpaytype.SelectedItem.Value == "0")
                return "1,2";
            else
                return orderpaytype.SelectedItem.Value;
        }
        //审核中
        protected void btnsh_Click(object sender, EventArgs e)
        {
            bindData(string.Format("orderstatus<>0 and orderstatus={0} and orderpaystatus='{1}'", 1, orderpaytype.SelectedItem.Value));
        }
        //已报料
        protected void btnbl_Click(object sender, EventArgs e)
        {
            bindData(string.Format("orderstatus<>0 and orderstatus={0} and orderpaystatus='{1}'", 2, orderpaytype.SelectedItem.Value));
        }
        //已到料
        protected void btndl_Click(object sender, EventArgs e)
        {
            bindData(string.Format("orderstatus<>0 and orderstatus={0} and orderpaystatus='{1}'", 3, orderpaytype.SelectedItem.Value));
        }
        //已生产
        protected void btnsc_Click(object sender, EventArgs e)
        {
            bindData(string.Format("orderstatus<>0 and orderstatus={0} and orderpaystatus='{1}'", 4, orderpaytype.SelectedItem.Value));
        }
        //已入库
        protected void btnrk_Click(object sender, EventArgs e)
        {
            bindData(string.Format("orderstatus<>0 and orderstatus={0} and orderpaystatus='{1}'", 7, orderpaytype.SelectedItem.Value));
        }
        //已发货
        protected void btnfh_Click(object sender, EventArgs e)
        {
            bindData(string.Format("orderstatus<>0 and orderstatus={0} and orderpaystatus='{1}'", 5, orderpaytype.SelectedItem.Value));
        }
        //已完成
        protected void btnwc_Click(object sender, EventArgs e)
        {
            bindData(string.Format("orderstatus<>0 and orderstatus={0} and orderpaystatus='{1}'", 6, orderpaytype.SelectedItem.Value));
        }

        protected void orderpaytype_SelectedIndexChanged(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            hidwhere.Value = string.Format("orderstatus<>0 and orderpaystatus={0}", orderpaytype.SelectedItem.Value);
            bindData(string.Format("orderstatus<>0 and orderpaystatus={0}", orderpaytype.SelectedItem.Value));
        }

    }
}